const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const tmplateRouter = require("./routes/template.js");
const api = require("./middleware/api.js");
const app = express();

mongoose.connect("mongodb://192.168.1.11:27017/template", { useNewUrlParser: true, useUnifiedTopology: true }).then((res) => {
    console.log("数据库链接成功");
}, (err) => {
    console.log("数据库链接失败", err);
});

app.use(api);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(api);

app.use("/xhr/v1", tmplateRouter);
// app.use("/xhr/v1", (req, res, next) => {
//     res.send("hello express $$");
// });

app.use((req, res, next) => {
    const err = new Error("Not Found");
    err.status = 404;
    next(err);
});

app.listen(3002, () => {
    console.log("server running on http://localhost:3002");
});

module.exports = app;